package com.org.any.autocar;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.autocar.reader.R;
import com.autocar.reader.bean.FriendAllBean;
import com.autocar.reader.bean.FriendSubBean;
import com.autocar.reader.util.ImageUtils;
import com.autocar.reader.util.NewDataToast;
import com.autocar.reader.view.XListView;

public class SearchResultAddressActivity extends BaseActivity {

	private ImageView backIv;
	private XListView aMainLv;
	private MyAdapter aMyadapter;
	private FriendAllBean bean;
	private int size;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_product_sub_layout);
		initIntent();
		initview();
		initproperty();
	}

	private void initIntent() {
		Intent intent = getIntent();
		bean = (FriendAllBean) intent.getSerializableExtra("bean");
		if (bean == null) {
			NewDataToast.makeText(getApplicationContext(),
					"get ProductDetailBean is null", false).show();
			finish();
		}
		Log.e("mg", "bean: " + bean.getNext_url());

		size = bean.getListBeans() == null ? 0 : bean.getListBeans().size();
	}

	private void initproperty() {

	}

	private void initview() {
		backIv = (ImageView) findViewById(R.id.search_product_sub_top_iv_back);
		backIv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoMain();
			}
		});

		aMainLv = (XListView) findViewById(R.id.search_product_sub_xlv_moods);

		aMyadapter = new MyAdapter();
		aMainLv.setAdapter(aMyadapter);
		aMainLv.removeHeader();
		aMainLv.removeFooter();

		this.aMainLv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				FriendSubBean bean = aMyadapter.getItem(arg2);
				gotoChat(bean.getUsername(), bean.getUid());

			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			gotoMain();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void gotoMain() {
		hideLoadPb();
		finish();
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.fade_in, R.anim.right_out_anim);
	}

	public class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return size;
		}

		public FriendSubBean getItem(int position) {
			return bean.getListBeans().get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		public View getView(final int position, View view, ViewGroup arg2) {
			ViewHolder viewHolder = null;
			if (view == null) {
				viewHolder = new ViewHolder();
				view = getLayoutInflater().inflate(
						R.layout.sreach_address_item, null);
				viewHolder.tvTitle = (TextView) view
						.findViewById(R.id.sreach_address_item_title);
				viewHolder.picIv = (ImageView) view
						.findViewById(R.id.sreach_address_item_img);
				view.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) view.getTag();
			}
			final FriendSubBean mContent = getItem(position);
			viewHolder.tvTitle.setText(bean.getListBeans().get(position)
					.getUsername());
			ImageUtils.fb.display(viewHolder.picIv, mContent.getPhoto());

			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					gotoChat(mContent.getUsername(), mContent.getUid());
				}
			});
			return view;
		}

		class ViewHolder {
			TextView tvTitle;
			ImageView picIv;
		}
	}

	public void gotoChat(String username, String uid) {
		Intent intent = new Intent(this, ChatListActivity.class);
		intent.putExtra("username", username);
		intent.putExtra("uid", uid);
		startActivity(intent);
		overridePendingTransition(R.anim.right_in_anim, R.anim.fade_out);
	}

}
